'use strict';

goog.provide('Blockly.Arduino.servo');

goog.require('Blockly.Arduino');

Blockly.Arduino.servo_move = function() {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  var value_degree = Blockly.Arduino.valueToCode(this, 'DEGREE', Blockly.Arduino.ORDER_ATOMIC);

  Blockly.Arduino.definitions_['define_servo'] = '#include <Servo.h>\n';
  Blockly.Arduino.definitions_['var_servo' + dropdown_pin] = 'Servo servo_' + dropdown_pin + ';\n';
  Blockly.Arduino.setups_['setup_servo_' + dropdown_pin] = 'servo_' + dropdown_pin + '.attach(' + dropdown_pin + ');\n';
  var code = 'servo_' + dropdown_pin + '.write(' + value_degree + ');\n';

  //  Blockly.Arduino.definitions_['define_servo'] = '#include <motor_servo.h>\n';
  // Blockly.Arduino.definitions_['var_servo' + dropdown_pin] = 'motor_servo servo_' + dropdown_pin + ';\n';
  // Blockly.Arduino.setups_['setup_servo_' + dropdown_pin] = 'servo_' + dropdown_pin + '.Servo_init(' + dropdown_pin + ');\n';
  //
  // var code = 'servo_' + dropdown_pin + '.servopulse('+dropdown_pin+',' + value_degree + ');\n';
  return code;
};


Blockly.Arduino.servo_read_degrees = function() {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);

  Blockly.Arduino.definitions_['define_servo'] = '#include <Servo.h>\n';
  Blockly.Arduino.definitions_['var_servo' + dropdown_pin] = 'Servo servo_'+dropdown_pin+';\n';
  Blockly.Arduino.setups_['setup_servo_' + dropdown_pin] = 'servo_' + dropdown_pin + '.attach(' + dropdown_pin + ');\n';

  var code = 'servo_' + dropdown_pin + '.read()';
  return code;
};


Blockly.Arduino.green_servo_move = function() {
  var dropdown_pin ="7";
  var value_degree = Blockly.Arduino.valueToCode(this, 'DEGREE', Blockly.Arduino.ORDER_ATOMIC);

  Blockly.Arduino.definitions_['define_servo'] = '#include <Servo.h>\n';
  Blockly.Arduino.definitions_['var_servo' + dropdown_pin] = 'Servo servo_' + dropdown_pin + ';\n';
  Blockly.Arduino.setups_['setup_servo_' + dropdown_pin] = 'servo_' + dropdown_pin + '.attach(' + dropdown_pin + ');\n';

  var code = 'servo_' + dropdown_pin + '.write(' + value_degree + ');\n';

  // Blockly.Arduino.definitions_['define_servo'] = '#include <motor_servo.h>\n';
  // Blockly.Arduino.definitions_['var_servo' + dropdown_pin] = 'motor_servo servo_' + dropdown_pin + ';\n';
  // Blockly.Arduino.setups_['setup_servo_' + dropdown_pin] = 'servo_' + dropdown_pin + '.Servo_init(' + dropdown_pin + ');\n';
  //
  // var code = 'servo_' + dropdown_pin + '.servopulse('+dropdown_pin+',' + value_degree + ');\n';
  return code;
};


Blockly.Arduino.white_servo_move = function() {
  var dropdown_pin ="11";
  var value_degree = Blockly.Arduino.valueToCode(this, 'DEGREE', Blockly.Arduino.ORDER_ATOMIC);

  Blockly.Arduino.definitions_['define_servo'] = '#include <Servo.h>\n';
  Blockly.Arduino.definitions_['var_servo' + dropdown_pin] = 'Servo servo_' + dropdown_pin + ';\n';
  Blockly.Arduino.setups_['setup_servo_' + dropdown_pin] = 'servo_' + dropdown_pin + '.attach(' + dropdown_pin + ');\n';

  var code = 'servo_' + dropdown_pin + '.write(' + value_degree + ');\n';
  // Blockly.Arduino.definitions_['define_servo'] = '#include <motor_servo.h>\n';
  // Blockly.Arduino.definitions_['var_servo' + dropdown_pin] = 'motor_servo servo_' + dropdown_pin + ';\n';
  // Blockly.Arduino.setups_['setup_servo_' + dropdown_pin] = 'servo_' + dropdown_pin + '.Servo_init(' + dropdown_pin + ');\n';
  //
  // var code = 'servo_' + dropdown_pin + '.servopulse('+dropdown_pin+',' + value_degree + ');\n';
  return code;
};

Blockly.Arduino.blue_servo_move = function() {
  var dropdown_pin ="8";
  var value_degree = Blockly.Arduino.valueToCode(this, 'DEGREE', Blockly.Arduino.ORDER_ATOMIC);

  Blockly.Arduino.definitions_['define_servo'] = '#include <Servo.h>\n';
  Blockly.Arduino.definitions_['var_servo' + dropdown_pin] = 'Servo servo_' + dropdown_pin + ';\n';
  Blockly.Arduino.setups_['setup_servo_' + dropdown_pin] = 'servo_' + dropdown_pin + '.attach(' + dropdown_pin + ');\n';

  var code = 'servo_' + dropdown_pin + '.write(' + value_degree + ');\n';
  // Blockly.Arduino.definitions_['define_servo'] = '#include <motor_servo.h>\n';
  // Blockly.Arduino.definitions_['var_servo' + dropdown_pin] = 'motor_servo servo_' + dropdown_pin + ';\n';
  // Blockly.Arduino.setups_['setup_servo_' + dropdown_pin] = 'servo_' + dropdown_pin + '.Servo_init(' + dropdown_pin + ');\n';
  //
  // var code = 'servo_' + dropdown_pin + '.servopulse('+dropdown_pin+',' + value_degree + ');\n';
  return code;
};


Blockly.Arduino.black_servo_move = function() {
  var dropdown_pin ="13";
  var value_degree = Blockly.Arduino.valueToCode(this, 'DEGREE', Blockly.Arduino.ORDER_ATOMIC);

  Blockly.Arduino.definitions_['define_servo'] = '#include <Servo.h>\n';
  Blockly.Arduino.definitions_['var_servo' + dropdown_pin] = 'Servo servo_' + dropdown_pin + ';\n';
  Blockly.Arduino.setups_['setup_servo_' + dropdown_pin] = 'servo_' + dropdown_pin + '.attach(' + dropdown_pin + ');\n';

  var code = 'servo_' + dropdown_pin + '.write(' + value_degree + ');\n';
  // Blockly.Arduino.definitions_['define_servo'] = '#include <motor_servo.h>\n';
  // Blockly.Arduino.definitions_['var_servo' + dropdown_pin] = 'motor_servo servo_' + dropdown_pin + ';\n';
  // Blockly.Arduino.setups_['setup_servo_' + dropdown_pin] = 'servo_' + dropdown_pin + '.Servo_init(' + dropdown_pin + ');\n';
  //
  // var code = 'servo_' + dropdown_pin + '.servopulse('+dropdown_pin+',' + value_degree + ');\n';
  return code;
};

Blockly.Arduino.red_servo_move = function() {
  var dropdown_pin ="4";
  var value_degree = Blockly.Arduino.valueToCode(this, 'DEGREE', Blockly.Arduino.ORDER_ATOMIC);

  Blockly.Arduino.definitions_['define_servo'] = '#include <Servo.h>\n';
  Blockly.Arduino.definitions_['var_servo' + dropdown_pin] = 'Servo servo_' + dropdown_pin + ';\n';
  Blockly.Arduino.setups_['setup_servo_' + dropdown_pin] = 'servo_' + dropdown_pin + '.attach(' + dropdown_pin + ');\n';

  var code = 'servo_' + dropdown_pin + '.write(' + value_degree + ');\n';
  // Blockly.Arduino.definitions_['define_servo'] = '#include <motor_servo.h>\n';
  // Blockly.Arduino.definitions_['var_servo' + dropdown_pin] = 'motor_servo servo_' + dropdown_pin + ';\n';
  // Blockly.Arduino.setups_['setup_servo_' + dropdown_pin] = 'servo_' + dropdown_pin + '.Servo_init(' + dropdown_pin + ');\n';
  //
  // var code = 'servo_' + dropdown_pin + '.servopulse('+dropdown_pin+',' + value_degree + ');\n';
  return code;
};
